<template>
    <el-dialog
            :title="dialogParams.title"
            :visible.sync="dialogParams.visible"
            :close-on-click-modal="false"
            width="600px"
            append-to-body>
        <el-form :model="formData" :rules="rules" :disabled="dialogParams.type === 'detail'" ref="fileForm" label-width="100px" class="demo-fileForm">
            <el-form-item label="参数名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入参数名称"></el-input>
            </el-form-item>
            <el-form-item label="编码" prop="code">
                <el-input v-model="formData.code" placeholder="请输入编码"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="descr">
                <el-input
                        type="textarea"
                        placeholder="请输入描述"
                        :autosize="{ minRows: 4, maxRows: 10}"
                        v-model="formData.descr"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('fileForm')">取 消</el-button>
            <el-button type="primary" @click="submit('fileForm')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import {addSubParams, editSubParams, getSubDictDetail } from "@/api/systemManage";

    export default {
        name: 'addSubParams',
        props: {
            dialogParams: {
                type: Object
            }
        },
        data () {
            return {
                formData: {
                    name: '',
                    code: '',
                    descr: ''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入参数名称', trigger: 'blur'}
                    ],
                    code: [
                        {required: true, message: '请输入编码', trigger: 'blur'}
                    ]
                }
            }
        },
        created () {
            this.getDetail()
        },
        methods: {
            // 查看详情
            getDetail () {
                if (this.dialogParams.id) {
                    getSubDictDetail(this.dialogParams.id).then(res => {
                        this.formData = {
                            name: res.name,
                            code: res.code,
                            descr: res.descr
                        }
                    }).catch(() => {})
                }
            },
            submit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let params = {};
                        switch (this.dialogParams.type) {
                            case 'add':
                                params = {
                                    ...this.formData,
                                    dictId: this.dialogParams.dictId
                                };
                                addSubParams(params).then(res => {
                                    if (res) {
                                        this.$message.success('操作成功');
                                        this.$emit('getList');
                                        this.dialogParams.visible = false;
                                    }
                                }).catch();
                                break;
                            case 'edit':
                                params = {
                                    id: this.dialogParams.id,
                                    data: this.formData
                                };
                                editSubParams(params.id, params.data).then(res => {
                                    if (res) {
                                        this.$message.success('操作成功');
                                        this.$emit('getList');
                                        this.dialogParams.visible = false;
                                    }
                                }).catch();
                                break;
                        }
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.dialogParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>